package com.kidback.bbloc259.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;

import com.kidback.bbloc259.app.KidApplication;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by meikai on 2020/06/03.
 */
public class ActivityLaunchUtils {

    public static boolean launch(Intent intent) {
        if (intent == null) {
            return false;
        }
        try {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            KidApplication.getInstance().startActivity(intent);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 避免各个简单Activity重复写launch()方法
     *
     * @param context
     * @param clz
     */
    public static void launch(Context context, Class<? extends Activity> clz) {
        launch(context, clz, null);
    }

    public static void launch(Context context, Class<? extends Activity> clz, Object param) {
        Intent intent = new Intent(context, clz);

        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        if (param != null) {
            EventBus.getDefault().postSticky(param);
        }
        context.startActivity(intent);
    }


    public static void launchForResult(Activity activity, int requestCode, Class<? extends Activity> clz) {
        Intent intent = new Intent(activity, clz);
        activity.startActivityForResult(intent, requestCode);
    }


    public static void launchWebView(Context context, String webpageUrl) {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri content_url = Uri.parse(webpageUrl);
        intent.setData(content_url);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        try {
            context.startActivity(intent);
        } catch (Exception e) {

        }

    }

    public static void launchWebView(Activity activity, String webpageUrl) {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri content_url = Uri.parse(webpageUrl);
        intent.setData(content_url);

        try {
            activity.startActivity(intent);
        } catch (Exception e) {

        }
    }

    /**
     * 回到桌面
     */
    public static void launchHome(Activity activity) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        try {
            activity.startActivity(intent);
        } catch (Exception e) {

        }
    }

    /**
     * 调起系统发短信功能
     *
     * @param phoneNumber
     * @param message
     */
    public static void launchSendSMSTo(Context context, String phoneNumber, String message) {
        try {
            Intent intent = new Intent(Intent.ACTION_SENDTO);
            if (StringUtils.isEmpty(phoneNumber)) {
                phoneNumber = "";
            }
            intent.setData(Uri.parse("smsto:" + phoneNumber));//必须设置
            if (StringUtils.isNotEmpty(message)) {
                intent.putExtra("sms_body", message);
            }
            if (!(context instanceof Activity)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            context.startActivity(intent);
        } catch (Exception e) {

        }
    }

    /**
     * 打开系统设置中此应用的设置界面
     */
    public static void openAppSettings() {
        try {
            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            Uri uri = Uri.fromParts("package", KidApplication.getInstance().getPackageName(), null);
            intent.setData(uri);
            KidApplication.getInstance().startActivity(intent);
        } catch (Exception e) {

        }
    }

    /**
     * 打开系统定位服务设置
     */
    public static void openLocSetting() {
        try {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            KidApplication.getInstance().startActivity(intent);
        } catch (Exception e) {

        }
    }

    /**
     * @param text  分享的具体内容，只支持文本；若链接，需要将链接拼接在文本内
     * @param title 手机系统弹框的标题栏所显示的内容，并不会跟随分享出去
     */
    public static void shareText(Context context, String text, String title) {
        try {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, text);
            context.startActivity(Intent.createChooser(intent, title));
        } catch (Exception e) {

        }
    }

}

